package com.baseframe.jwt.utils;


import com.baseframe.jwt.constant.BaseJwtConstant;
import com.baseframe.jwt.props.BaseJwtProperties;
import com.baseframe.tool.utils.spring.SpringUtil;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;

import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.Random;

public class JwtUtil {
    public  static String SIGN_KEY;

    public static String getSignKey() {

        BaseJwtProperties jwtProperties = SpringUtil.getBean(BaseJwtProperties.class);
        SIGN_KEY = jwtProperties.getSignKey();
        return SIGN_KEY;
    }

    /**
     * spring环境只外需要做测试，先调用此方法将singKey设置一下
     * @param signKey jwt签名密钥
     */
    public static void load(String signKey){
        SIGN_KEY = signKey;
    }




    public static Claims getClaims(String token){
        return Jwts.parserBuilder()
                .setSigningKey(Base64.getDecoder().decode(getBase64Security())).build()
                .parseClaimsJws(token).getBody();

    }

    public static String getJwtId(String token){
        Claims claims = getClaims(token);
        return claims.getId();
    }

    /**
     * 生成singKey
     * @param length
     * @return
     */
    private static String generateSingKey(int length){
        if (length<32){
            length=32;
        }
        StringBuilder sb = new StringBuilder();
        Random random = new Random();
        for (int i = 0; i < length; i++) {
            int randomInt = random.nextInt(26);
            char character = (char) (randomInt+97);
            String value = String.valueOf(character);
            sb.append(value);
        }
        return sb.toString();
    }

    public static String getBase64Security() {
        String signKey = "";
        if (SIGN_KEY==null){
            signKey = getSignKey();
        }else {
            signKey = SIGN_KEY;
        }
        return Base64.getEncoder().encodeToString(signKey.getBytes(StandardCharsets.UTF_8));
    }






    public static  <T> T parse(String token,Class<T> clazz){


        return null;
    }

    public static void main(String[] args) {
        load(BaseJwtConstant.DEFAULT_SIGN_KEY);
        String token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX2lkIjoxNTY0NDEyMzAzNTIwMDEsInJvbGVfaWQiOjE1NjQzNjYyNzg2MDAxLCJ1c2VyX25hbWUiOiJyb290Iiwic2NvcGUiOlsiYWxsIl0sImRldGFpbHMiOnt9LCJleHAiOjE2ODQ2NDY0OTIsImF1dGhvcml0aWVzIjpbInBlcm1pc3Npb25fY2hlY2tzX2xpc3QiLCJwZXJtaXNzaW9uX2RlbGV0ZSIsInJvbGVfdXBkYXRlIiwicm9sZV9hZGQiLCJyb2xlX2FuZF9wZXJtaXNzaW9uX2NoZWNrc19saXN0IiwicGVybWlzc2lvbl9hZGQiLCJwZXJtaXNzaW9uX3VwZGF0ZSIsInJvbGVfZGVsZXRlIl0sImp0aSI6ImE0MzNlZDAxLWJjZTgtNDk1Yi1hMmM5LTFmZTcwZDkyMWE2ZiIsImNsaWVudF9pZCI6ImMxIn0.Ra2tE7AQHAEWq3NsnsxXZ2mmhRcjoFvdClSC296I6hs";
//        UserBean parse = parse(token,UserBean.class);
//        UserBean parse = TokenUtil.parse(token);
        System.out.println(JwtUtil.getJwtId(token));
//        Claims claims = getClaims(token);
//        System.out.println(claims.toString());//1684646492
    }



}
